package com.afd.ozblip.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.afd.ozblip.adapter.ChatMsgListAdapter;
import com.afd.ozblip.chat.ChatClient;
import com.afd.ozblip.entity.ChatEntity;
import com.afd.ozblip.entity.UserEntity;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class UserChatActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private TextView friendNameTv;
	private ListView chatListView;
	private EditText contentEt;
	private Button sendBtn;
	private LinearLayout commonRight;

	/****************** 数据变量 **************************/
	private ChatMsgListAdapter chatListAdapter;// 消息视图的Adapter
	private List<ChatEntity> chatList = new ArrayList<ChatEntity>();// 消息对象数组
	private String toUserId;
	private UserEntity friendInfo;
	private String contentStr;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_chat);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 初始化数据
		initData();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.commonRight = (LinearLayout) findViewById(R.id.ll_common_right);
		this.friendNameTv = (TextView) findViewById(R.id.tv_chat_friendName);
		this.chatListView = (ListView) findViewById(R.id.lv_chat_msg_list);
		this.contentEt = (EditText) findViewById(R.id.et_chat_input);
		this.sendBtn = (Button) findViewById(R.id.btn_chat_send);

	}

	private Handler chatHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constants.UpdateLV:
				String content = (String) msg.obj;
				// 收到的消息
				ChatEntity entity = new ChatEntity("", "", content, null, true);
				chatList.add(entity);
				chatListAdapter.notifyDataSetChanged();// 通知ListView，数据已发生改变
				chatListView.setSelection(chatListView.getCount() - 1);// 发送一条消息时，ListView显示选择最后一项
				break;

			default:
				break;
			}
		}
	};

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener chatClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_common_right:
					Intent businessIntent = new Intent(UserChatActivity.this,
							PeopleDetailActivity.class);
					businessIntent.putExtra("userInfo", friendInfo);
					startActivity(businessIntent);
					break;
				case R.id.btn_chat_send:
					contentStr = contentEt.getText().toString();
					if (StringUtil.isNullOrEmpty(contentStr)) {
						showShortToast(UserChatActivity.this, "请你输入内容", true);
						return;
					}
					new Thread(new Runnable() {
						public void run() {
							ChatClient chat = ChatClient
									.getInstances(commonUserInfo.getUserId());
							chat.sendMsgToUser(toUserId, contentStr);
							chat.setUIHandler(chatHandler);
						}
					}).start();
					ChatEntity entity = new ChatEntity();
					entity.setMessage(contentStr);
					entity.setMsgType(false);

					// messageDB.saveMsg(user.getId(), entity);

					chatList.add(entity);
					chatListAdapter.notifyDataSetChanged();// 通知ListView，数据已发生改变
					contentEt.setText("");// 清空编辑框数据
					chatListView.setSelection(chatListView.getCount() - 1);// 发送一条消息时，ListView显示选择最后一项

					break;

				default:
					break;
				}
			}
		};
		this.commonRight.setOnClickListener(chatClick);
		this.sendBtn.setOnClickListener(chatClick);
	}

	/**
	 * 加载消息历史，从数据库中读出
	 */
	private void initData() {
		// List<ChatMsgEntity> list = messageDB.getMsg(user.getId());
		// if (list.size() > 0) {
		// for (ChatMsgEntity entity : list) {
		// if (entity.getName().equals("")) {
		// entity.setName(user.getName());
		// }
		// if (entity.getImg() < 0) {
		// entity.setImg(user.getImg());
		// }
		// mDataArrays.add(entity);
		// }
		// Collections.reverse(mDataArrays);
		// }
		chatListAdapter = new ChatMsgListAdapter(this, chatList);
		chatListView.setAdapter(chatListAdapter);
		chatListView.setSelection(chatListAdapter.getCount() - 1);
		friendNameTv.setText(friendInfo.getUserName());
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
		this.friendInfo = (UserEntity) intent
				.getSerializableExtra("friendInfo");
		this.toUserId = friendInfo.getUserId();
	}
}